<?php include_once ('function/check_login_function.php'); ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>


<?php include_once('nav.php'); ?>
      
    <div class="container">
    <div class="row">
    <div class="span5">
      <h1 style="padding-left: 20px;">Update your profile</h1>
	   <div class="alert alert-info">
					<?php
						if (isset($_SESSION['message'])) {
							foreach ($_SESSION['message'] as $message) {
								echo constant($message) . '<br />';
							}
							unset($_SESSION['message']);
						}
					?>
					<form action="function/update_function.php" method="POST"  class="form-inline">
					
					<div class="control-group"><label class="control-label"  for="password">Old Password:</label><div class="controls"> <input type="password" class="input-large"  id="passwordOld" name="passwordOld"/> </div></div> 
					<div class="control-group"><label class="control-label"  for="password">New Password:</label><div class="controls"> <input type="password" class="input-large"  id="password1" name="password1"/> </div></div> 

					<div class="control-group"><label class="control-label"  for="password">Retype New Password:</label><div class="controls"> <input type="password" class="input-large"  id="password2" name="password2"/> </div></div>
					
                        <button type="submit" name="changePassword" class="btn btn-primary">Change password</button><br /><br />
					
						<?php if (!$user->hasRequestEmailChange()) {?>
						<div class="control-group"><label class="control-label"  for="password">New e-mail:</label><div class="controls"> <input type="email" class="input-large"  id="email" name="email"/> </div></div>
							<button type="submit" name="changeEmail" class="btn btn-primary">Change e-mail</button><br /><br />
						<?php } else {
							 echo constant("MSG_EMAIL_CHANGE_REQUESTED") . " Email: " . $user->getNewEmail() 
						?>
							<button type="submit" name="removeChangeEmail" class="btn btn-primary" style="margin:10px;">Remove e-mail change</button>
						<?php } ?>
						
					<div class="control-group">
						<label class="control-label" for="firstname">First name:</label> 
						<div class="controls"> 
							<input type="text" class="input-large"  style="height:26px" id="firstname" name="firstname" value="<?php echo $user->getFirstname()?>"/>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label"  for="lastname">Last name:</label> 
						<div class="controls"> 
						<input type="text"class="input-large"  style="height:26px" id="lastname" name="lastname" value="<?php echo $user->getLastname()?>" />  <br />
					
					</div></div><div class="control-group"><label class="control-label"  for="gender">Gender:</label><div class="controls"> 
					<select id="gender" name="gender">
						<?php
							if ($user->getGender() === 'male') {
								echo '<option value="male" selected="selected">Male</option>';
								echo '<option value="female">Female</option>';
							} else {
								echo '<option value="male">Male</option>';
								echo'<option value="female" selected="selected">Female</option>';
							}
						?>
					</select> <br />
					</div></div>
				
				<?php 
					$address = explode('|', $user->getAddress());
					$address1 = $address[0];
					$address2 = $address[1];
					$city = $address[2];
					$state = $address[3];
					$country = $address[4];
					$postal = $address[5];
				?>
				<div class="alert alert-success">
					<div class="control-group"><label class="control-label"  for="address">Address 1:</label><div class="controls"> <input type="text" class="input-large"  id="address1" name="address1" value="<?php echo $address1 ?>"/> </div></div>
					<div class="control-group"><label class="control-label"  for="address">Address 2:</label><div class="controls"> <input type="text" class="input-large"  id="address2" name="address2" value="<?php echo $address2 ?>"/> </div></div>
					<div class="control-group"><label class="control-label"  for="address">City:</label><div class="controls"> <input type="text" class="input-large"  id="city" name="city" value="<?php echo $city ?>"/> </div></div>
					<div class="control-group"><label class="control-label"  for="address">State:</label><div class="controls"> <input type="text" class="input-large"  id="state" name="state" value="<?php echo $state ?>"/> </div></div>
					<div class="control-group"><label class="control-label"  for="address">Country:</label><div class="controls"> <input type="text" class="input-large"  id="country" name="country" value="<?php echo $country ?>"/> </div></div>
					<div class="control-group"><label class="control-label"  for="address">Postal code:</label><div class="controls"> <input type="text" class="input-large"  id="postal" name="postal" value="<?php echo $postal ?>"/> </div></div>

				</div>
					
					<div class="control-group"><label class="control-label"  for="contact">Contact:</label> <div class="controls"> 
					<input type="text" class="input-large"  style="height:26px" id="contact" name="contact" value="<?php echo $user->getContact()?>" /> <br /> <br/>		

					
					<?php 
						$dob = explode("-", $user->getDoB());
						$year = intval($dob[0]);
						$month = intval($dob[1]);
						$day = intval($dob[2]);
					?>
					<div class="control-group"><label class="control-label"  for="day">Day:</label><div class="controls">
                    <select id="day" name="day">
					<?php for ($i=1; $i<=31; $i++) {
						if ($i === $day) {
							echo '<option value="'.$i.'" selected="selected">'.str_pad($i, 2, "0", STR_PAD_LEFT).'</option>';
						} else {
							echo '<option value="'.$i.'">'.str_pad($i, 2, "0", STR_PAD_LEFT).'</option>';
						}
					}?>
					</select></div></div>
					<div class="control-group"><label class="control-label"  for="month">Month:</label><div class="controls">
					<select id="month" name="month">
					<?php for ($i=1; $i<=12; $i++) {
						if ($i === $month) {
							echo '<option value="'.$i.'" selected="selected">'.str_pad($i, 2, "0", STR_PAD_LEFT).'</option>';
						} else {
							echo '<option value="'.$i.'">'.str_pad($i, 2, "0", STR_PAD_LEFT).'</option>';
						}
						
					}?>
					</select>	</div></div>
					<div class="control-group"><label class="control-label"  for="year">Year:</label><div class="controls">
					<select id="year" name="year">
					<?php for ($i=2012; $i>=1900; $i--) {
						if ($i === $year) {
							echo '<option value="'.$i.'" selected="selected">'.str_pad($i, 2, "0", STR_PAD_LEFT).'</option>';	
						} else {
							echo '<option value="'.$i.'">'.str_pad($i, 2, "0", STR_PAD_LEFT).'</option>';	
						}
					}?>
					</select> </div></div>
					
					
					<?php if ($user->getRole() === 'patient') { ?>
					</div></div><div class="control-group"><label class="control-label"  for="bloodtype">Blood Type:</label><div class="controls">  
					<input type="text" class="input-large"  style="height:26px" id="bloodtype" name="bloodtype" value="<?php echo $user->getBloodtype()?>" /> <br />
					</div></div><div class="control-group"><label class="control-label"  for="allergy">Allergy:</label> <div class="controls"> 
					<input type="text" class="input-large"  style="height:26px" id="allergy" name="allergy" value="<?php echo $user->getAllergy()?>" /> <br />
                        <button type="submit" name="update" class="btn btn-primary" style="margin:10px;">Update</button>

					<?php } else if ($user->getRole() === 'doctor') { ?></div></div>
                        <button type="submit" name="update" class="btn btn-primary pull-right">Update</button>
					<?php
					$hospital_list = $user->getHospitalList();
					if ($hospital_list !== null) {?>
						<h3>List of Hospitals</h3>
						<table class="table table-striped" style="margin-left:40px;">
						<tr>
						<th>ID</th>
						<th>Name</th>
						<th>Address</th>
						<th>Contact</th>
						<th></th>
						</tr>
						<?php
								foreach ($hospital_list as $hospital) {
									echo '<tr>';
									echo '<td>' . $hospital['hospital_id'] . '</td>';
									echo '<td>' . $hospital['hospital_name'] . '</td>';
									echo '<td>' . $hospital['hospital_address'] . '</td>';
									echo '<td>' . $hospital['hospital_contact'] . '</td>';
									echo '<td>';
									echo '<form action="function/update_function.php" method="POST">';
									echo '<input type="hidden" id="hospitalID" name="hospitalID" value="' . $hospital['hospital_id'] . '"/>';
									echo '<input type="hidden" id="from" name="from" value="edit.php"/>';
									echo '<button type="submit" name="unattachHospital" class="btn btn-primary">Unattach</button>';
									echo '</form>';
									echo '</tr>';
								}
							}
						?>
						</table>
						
					<h3 style="padding-left: 20px;">Hospital</h3>
					
					<div class="hospital" style="border:1px dotted black;padding:5px">
						<div class="control-group"><label for="hospital">Hospital</label>
						<div class="controls">
						<?php 
						$hospital_list = Hospital::getHospitalList();
							if ($hospital_list !== null) {
						?>
						<select id="hospital" name="hospital">
						<option value="0">Select hospital</option>
						<?php 

								foreach ($hospital_list as $hospital) {
									echo '<option value="' . $hospital['hospital_id'] . '">' . $hospital['hospital_name'] . '</option>';
								}
						?>
						<option value="0">Others</option>
						</select> </div></div>
						<?php } ?>
						<div class="control-group"><label for="hospitalName">Name:</label> 
						<div class="controls"><input type="text" id="hospitalName" name="hospitalName" /> </div></div>
						<div class="control-group"><label for="hospitalAddress">Address:</label> 
						<div class="controls"><input type="text" id="hospitalAddress" name="hospitalAddress" /> </div></div>
						<div class="control-group"><label for="hospitalContact">Contact:</label> 
						<div class="controls"><input type="text" id="hospitalContact" name="hospitalContact" /> </div></div>
					</div>
					
                        <button type="submit" class="btn btn-primary" name="addHospital" value="Add Hospital" style="margin:10px;">Add Hospital</button>
					<?php }  else {?> 
                        <button type="submit" name="update" class="btn btn-primary" style="margin:10px;">Update</button>
					<?php } ?>

                    </form>
    </div>
    </div>
    </div>
    </div> <!-- /container -->
  </body>
</html>